package
{
	import flash.utils.*;
	
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	
	import items.*;
	
	/**
	 * ...
	 * @author JAC
	 */
	public final class UI extends FlxGroup
	{
		public static const HEIGHT:uint = 75;
		
		private var _bg:FlxSprite;
		
		public var _itemOne:Item;
		private var _itemTwo:Item;
		private var _itemThree:Item;
		
		private var _itemOneButton:FlxSprite;
		private var _itemTwoButton:FlxSprite;
		private var _itemThreeButton:FlxSprite;
		
		private var _scoreText:FlxText;
		private var _minScoreText:FlxText;
		private var _previousScore:int;
		private var _scoreColorTimer:FlxDelay;
		private var _livesText:FlxText;
		
		private var _waveText:FlxText;
		private var _maxWaveText:FlxText;
		
		private var _itemOneBG:FlxSprite;
		private var _itemTwoBG:FlxSprite;
		private var _itemThreeBG:FlxSprite;
		
		private var _itemOneFinger:FlxSprite;
		private var _itemTwoFinger:FlxSprite;
		private var _itemThreeFinger:FlxSprite;
		
		private var _itemOneBar:FlxBar;
		private var _itemTwoBar:FlxBar;
		private var _itemThreeBar:FlxBar;
		
		public function UI()
		{
			_bg = new FlxSprite(0, FlxG.height - 75, Assets.ImgUIBar);
			add(_bg);
			
			initScoreText();
			initLivesText();
			initWaveText();
			initInventory();
			initBars();
		}
		
		override public function update():void
		{
			super.update();
			
			_itemOneButton.frame = FlxG.keys.A ? 1 : 0;
			_itemTwoButton.frame = FlxG.keys.S ? 3 : 2;
			_itemThreeButton.frame = FlxG.keys.D ? 5 : 4;
			
			if (Player.isAlive)
			{
				if (FlxG.keys.justPressed("A"))
				{
					useItemOne();
				}
				if (FlxG.keys.justPressed("S"))
				{
					useItemTwo();
				}
				if (FlxG.keys.justPressed("D"))
				{
					useItemThree();
				}
			}
			
			checkItemFingers();
			checkItemBars();
			updateScoreText();
			updateLivesText();
			updateWaveText();
		}
		
		public function resetText():void
		{
			_previousScore = Tracker.score;
			
			_minScoreText.text = Tracker.minScore.toString();
			_scoreText.text = "0";
			
			if (Tracker.minScore > Tracker.score)
			{
				_scoreText.color = 0xFFFF0000;
			}
			else if (Tracker.minScore < Tracker.score)
			{
				_scoreText.color = 0xFF00FF00;
			}
			else
			{
				_scoreText.color = 0xFFFFFFFF;
			}
			
			_livesText.text = "0";
		}
		
		public function clearInventory():void
		{
			if (_itemOne != null)
			{
				removeFromSlotOne();
			}
			if (_itemTwo != null)
			{
				removeFromSlotTwo();
			}
			if (_itemThree != null)
			{
				removeFromSlotThree();
			}
			_itemOneFinger.visible = false;
			_itemTwoFinger.visible = false;
			_itemThreeFinger.visible = false;
			_itemOneBar.visible = false;
			_itemTwoBar.visible = false;
			_itemThreeBar.visible = false;
		}
		
		public function insertItem(i:Item):Boolean
		{
			var itemClass:String = getQualifiedClassName(i);
			
			if (_itemOne != null && getQualifiedClassName(_itemOne) == itemClass)
			{
				Animation.greenBoxTile(new FlxPoint(92, 550));
				i.destroy();
				_itemOne.size++;
				return true;
			}
			else if (_itemTwo != null && getQualifiedClassName(_itemTwo) == itemClass)
			{
				Animation.greenBoxTile(new FlxPoint(163, 550));
				i.destroy();
				_itemTwo.size++;
				return true;
			}
			else if (_itemThree != null && getQualifiedClassName(_itemThree) == itemClass)
			{
				Animation.greenBoxTile(new FlxPoint(234, 550));
				i.destroy();
				_itemThree.size++;
				return true;
			}
			
			if (_itemOne == null)
			{
				addToSlotOne(i);
				Animation.greenBoxTile(new FlxPoint(92, 550));
				return true;
			}
			else if (_itemTwo == null)
			{
				addToSlotTwo(i);
				Animation.greenBoxTile(new FlxPoint(163, 550));
				return true;
			}
			else if (_itemThree == null)
			{
				addToSlotThree(i);
				Animation.greenBoxTile(new FlxPoint(234, 550));
				return true;
			}
			else
			{
				return false;
			}
		}
		
		private function initWaveText():void
		{
			_waveText = new FlxText(300, 562, 50, "0");
			_waveText.font = "cooper_black";
			_waveText.size = 20;
			add(_waveText);
			
			_maxWaveText = new FlxText(360, 562, 50, "0");
			_maxWaveText.font = "cooper_black";
			_maxWaveText.size = 20;
			add(_maxWaveText);
		}
		
		private function updateWaveText():void
		{
			_waveText.text = Tracker.wave.toString();
			_maxWaveText.text = Tracker.maxWave.toString();
		}
		
		private function initScoreText(levelName:String = null):void
		{
			_minScoreText = new FlxText(605, 533, FlxG.width * 2 / 15, "0");
			_minScoreText.font = "cooper_black";
			_minScoreText.size = 20;
			add(_minScoreText);
			
			_scoreText = new FlxText(605, 565, FlxG.width * 2 / 15, "0");
			_scoreText.font = "cooper_black";
			_scoreText.size = 20;
			add(_scoreText);
			
			_scoreColorTimer = new FlxDelay(1500);
			
			if (levelName != null)
			{
				var name:FlxText = new FlxText(400, 550, FlxG.width * 2 / 15, levelName);
				name.font = "cooper_black";
				name.size = 30;
				add(name);
			}
			
			_previousScore = Tracker.score;
		}
		
		private function updateScoreText():void
		{
			Tracker.tallyTreasures();
			
			if (_previousScore != Tracker.score)
			{
				_scoreColorTimer.start();
				if (_previousScore > Tracker.score)
				{
					_scoreText.color = 0xFFFF4200;
					FlxG.shake(0.001, 0.5);
				}
				else
				{
					_scoreText.color = 0xFFFFFF00;
				}
			}
			
			if (_scoreColorTimer.hasExpired)
			{
				if (Tracker.minScore > Tracker.score)
				{
					_scoreText.color = 0xFFFF0000;
				}
				else if (Tracker.minScore < Tracker.score)
				{
					_scoreText.color = 0xFF00FF00;
				}
				else
				{
					_scoreText.color = 0xFFFFFFFF;
				}
			}
			
			_scoreText.text = Tracker.score.toString();
			_previousScore = Tracker.score;
		}
		
		private function initLivesText():void
		{
			_livesText = new FlxText(705, 562, FlxG.width * 2 / 20, "0");
			_livesText.font = "cooper_black";
			_livesText.size = 20;
			add(_livesText);
		}
		
		private function updateLivesText():void
		{
			_livesText.text = Tracker.lives.toString();
		}
		
		private function checkItemFingers():void
		{
			if (_itemOneFinger.finished)
			{
				_itemOneFinger.visible = false;
			}
			if (_itemTwoFinger.finished)
			{
				_itemTwoFinger.visible = false;
			}
			if (_itemThreeFinger.finished)
			{
				_itemThreeFinger.visible = false;
			}
		}
		
		private function checkItemBars():void
		{
			if (_itemOne != null)
			{
				if (_itemOne.used)
				{
					if (_itemOne.secondsRemaining == 0 || !Player.isAlive)
					{
						removeFromSlotOne();
					}
					else
					{
						_itemOneBar.currentValue = _itemOne.secondsRemaining;
					}
				}
			}
			
			_itemOneBar.visible = _itemOne == null ? false : _itemOne.used;
			
			if (_itemTwo != null && _itemTwo.used)
			{
				if (_itemTwo.used)
				{
					if (_itemTwo.secondsRemaining == 0 || !Player.isAlive)
					{
						
						removeFromSlotTwo();
					}
					else
					{
						_itemTwoBar.currentValue = _itemTwo.secondsRemaining;
					}
				}
			}
			
			_itemTwoBar.visible = _itemTwo == null ? false : _itemTwo.used;
			
			if (_itemThree != null && _itemThree.used)
			{
				if (_itemThree.used)
				{
					if (_itemThree.secondsRemaining == 0 || !Player.isAlive)
					{
						removeFromSlotThree();
					}
					else
					{
						_itemThreeBar.currentValue = _itemThree.secondsRemaining;
					}
				}
			}
			
			_itemThreeBar.visible = _itemThree == null ? false : _itemThree.used;
		}
		
		private function addToSlotOne(i:Item):void
		{
			_itemOne = i;
			_itemOne.x = 92;
			_itemOne.y = 550;
			_itemOneBG.visible = true;
			add(_itemOne);
			_itemOneFinger.visible = true;
			_itemOneFinger.play("default", true);
		}
		
		private function addToSlotTwo(i:Item):void
		{
			_itemTwo = i;
			_itemTwo.x = 163;
			_itemTwo.y = 550;
			_itemTwoBG.visible = true;
			add(_itemTwo);
			_itemTwoFinger.visible = true;
			_itemTwoFinger.play("default", true);
		}
		
		private function addToSlotThree(i:Item):void
		{
			_itemThree = i;
			_itemThree.x = 234;
			_itemThree.y = 550;
			_itemThreeBG.visible = true;
			add(_itemThree);
			_itemThreeFinger.visible = true;
			_itemThreeFinger.play("default", true);
		}
		
		private function useItemOne():void
		{
			if (_itemOne == null)
			{
				return;
			}
			else if (!_itemOne.used)
			{
				if (_itemOne.onUse(Player.player))
				{
					Tracker.itemsUsed++;
					_itemOne.logUsage();
					_itemOneFinger.visible = false;
					_itemOneBar.setRange(0, _itemOne.duration);
				}
			}
		}
		
		private function useItemTwo():void
		{
			if (_itemTwo == null)
			{
				return;
			}
			else if (!_itemTwo.used)
			{
				if (_itemTwo.onUse(Player.player))
				{
					Tracker.itemsUsed++;
					_itemTwo.logUsage();
					_itemTwoFinger.visible = false;
					_itemTwoBar.setRange(0, _itemTwo.duration);
				}
			}
		}
		
		private function useItemThree():void
		{
			if (_itemThree == null)
			{
				return;
			}
			else if (!_itemThree.used)
			{
				if (_itemThree.onUse(Player.player))
				{
					Tracker.itemsUsed++;
					_itemThree.logUsage();
					_itemThreeFinger.visible = false;
					_itemThreeBar.setRange(0, _itemThree.duration);
				}
			}
		}
		
		private function removeFromSlotOne():void
		{
			if (_itemOne.size > 1)
			{
				_itemOne.destroy();
				return;
			}
			
			remove(_itemOne);
			_itemOne = null;
			_itemOneBG.visible = false;
			_itemOneFinger.visible = false;
		}
		
		private function removeFromSlotTwo():void
		{
			if (_itemTwo.size > 1)
			{
				_itemTwo.destroy();
				return;
			}
			
			remove(_itemTwo);
			_itemTwo = null;
			_itemTwoBG.visible = false;
			_itemThreeFinger.visible = false;
		}
		
		private function removeFromSlotThree():void
		{
			if (_itemThree.size > 1)
			{
				_itemThree.destroy();
				return;
			}
			
			remove(_itemThree);
			_itemThree = null;
			_itemThreeBG.visible = false;
			_itemThreeFinger.visible = false;
		}
		
		private function initInventory():void
		{
			_itemOne = null;
			_itemTwo = null;
			_itemThree = null;
			
			_itemOneButton = new FlxSprite(78, 537);
			_itemOneButton.loadGraphic(Assets.ImgButtonASD, true);
			_itemOneButton.addAnimation("press", [1, 0], 12, false);
			_itemOneButton.frame = 0;
			_itemTwoButton = new FlxSprite(149, 537);
			_itemTwoButton.loadGraphic(Assets.ImgButtonASD, true);
			_itemTwoButton.addAnimation("press", [3, 2], 12, false);
			_itemTwoButton.frame = 2;
			_itemThreeButton = new FlxSprite(220, 537);
			_itemThreeButton.loadGraphic(Assets.ImgButtonASD, true);
			_itemThreeButton.addAnimation("press", [5, 4], 12, false);
			_itemThreeButton.frame = 4;
			
			add(_itemOneButton);
			add(_itemTwoButton);
			add(_itemThreeButton);
			
			_itemOneBG = new FlxSprite(80, 538);
			_itemOneBG.makeGraphic(50, 50, 0xFFFFFF00);
			_itemOneBG.alpha = 0.2;
			_itemOneBG.visible = false;
			_itemTwoBG = new FlxSprite(151, 538);
			_itemTwoBG.makeGraphic(50, 50, 0xFFFFFF00);
			_itemTwoBG.alpha = 0.2;
			_itemTwoBG.visible = false;
			_itemThreeBG = new FlxSprite(222, 538);
			_itemThreeBG.makeGraphic(50, 50, 0xFFFFFF00);
			_itemThreeBG.alpha = 0.2;
			_itemThreeBG.visible = false;
			
			add(_itemOneBG);
			add(_itemTwoBG);
			add(_itemThreeBG);
			
			_itemOneFinger = new FlxSprite(80, FlxG.height - 100);
			_itemOneFinger.loadGraphic(Assets.ImgFinger, true);
			_itemOneFinger.addAnimation("default", [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 6, false);
			_itemOneFinger.visible = false;
			
			_itemTwoFinger = new FlxSprite(151, FlxG.height - 100);
			_itemTwoFinger.loadGraphic(Assets.ImgFinger, true);
			_itemTwoFinger.addAnimation("default", [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 6, false);
			_itemTwoFinger.visible = false;
			
			_itemThreeFinger = new FlxSprite(222, FlxG.height - 100);
			_itemThreeFinger.loadGraphic(Assets.ImgFinger, true);
			_itemThreeFinger.addAnimation("default", [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 6, false);
			_itemThreeFinger.visible = false;
			
			add(_itemOneFinger);
			add(_itemTwoFinger);
			add(_itemThreeFinger);
		}
		
		private function initBars():void
		{
			_itemOneBar = new FlxBar(80, FlxG.height - 8, FlxBar.FILL_LEFT_TO_RIGHT, 51, 5);
			_itemOneBar.visible = false;
			
			_itemTwoBar = new FlxBar(151, FlxG.height - 8, FlxBar.FILL_LEFT_TO_RIGHT, 51, 5);
			_itemTwoBar.visible = false;
			
			_itemThreeBar = new FlxBar(222, FlxG.height - 8, FlxBar.FILL_LEFT_TO_RIGHT, 51, 5);
			_itemThreeBar.visible = false;
			
			add(_itemOneBar);
			add(_itemTwoBar);
			add(_itemThreeBar);
		}
	}
}